package com.hns.can.master.web;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.common.domain.GridPage;
import com.hns.can.common.entity.BaseEntity;
import com.hns.can.common.web.BaseController;
import com.hns.can.master.entity.BaseEqmtInfoEntity;
import com.hns.can.master.service.EquipmentInfoService;
import com.hns.can.master.vo.EqtPostDataVo;
import com.hns.frame.mybatis.Pagination;
import com.hns.tool.export.ExportUtil;

/**
 * 设备信息维护控制类
 * @author:zhangyu
 * @email:zhangyu2222z@sina.cn
 * @version Revision 2.0.0
 */
@Controller
@RequestMapping("/equipmentInfo")
public class EquipmentInfoController extends BaseController {
    
	/**
	 * 设备信息service
	 */
    @Autowired
    private EquipmentInfoService equipmentInfoService;
    
    /**
	 * 导出文件表头
	 */
    private final String[] tableHeaderNames={"设备ID","SIM卡号","版本名称","所属机构","关联车辆","生产厂家","购买日期","设备状态","备注"};
    /**
	 * 导出文件字段名
	 */
    private final String[] tableHeaderFields = {"eqmtCd","simCardno","vrsnName","organName","licPltNo","produceFacty","prchDate","eqmtSt","remark"};
    
    /**
     * 添加一个设备
     * @param entity
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/addEquipmentInfo")
    @ResponseBody
    public Object addEquipmentInfo(BaseEqmtInfoEntity entity) throws Exception {
        equipmentInfoService.addEquipmentInfo(entity, this.getCurrentUser().getUser());
        return this.getReturnMsg("您成功添加一个设备");
    }
    
    /**
     * 得到设备的page对象
     * @param dataVo
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/searchEquipmentInfoPage")
    @ResponseBody
    public Object searchEquipmentInfoPage(EqtPostDataVo dataVo) throws Exception {
        SysUserEntity user = this.getCurrentUser().getUser();
        dataVo.setUserId(user.getId());
        Pagination page = equipmentInfoService.searchEquipmentInfoPage(dataVo);
        GridPage grid = new GridPage(page);
        return this.getReturnResult(grid);
    }
    
    /**
     * 导出设备信息excel
     * @param dataVo
     * @param response
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/exportEquipmentInfoExcel")
    @ResponseBody
    public void exportEquipmentInfoExcel(EqtPostDataVo dataVo,HttpServletResponse response) throws Exception {
        dataVo.setUserId(this.getCurrentUser().getUser().getId());
        List<BaseEntity> list = equipmentInfoService.getEquipmentInfoList(dataVo);
        ExportUtil.exportFiles(Arrays.asList(tableHeaderNames), Arrays.asList(tableHeaderFields), list, response, "excel", "equipmentInfo", "设备基础信息");
    }
    
    /**
     * 导出设备信息pdf
     * @param dataVo
     * @param response
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/exportEquipmentInfoPdf")
    @ResponseBody
    public void exportEquipmentInfoPdf(EqtPostDataVo dataVo, HttpServletResponse response) throws Exception {
        dataVo.setUserId(this.getCurrentUser().getUser().getId());
        List<BaseEntity> list = equipmentInfoService.getEquipmentInfoList(dataVo);
        ExportUtil.exportFiles(Arrays.asList(tableHeaderNames), Arrays.asList(tableHeaderFields), list, response, "pdf", "equipmentInfo", "设备基础信息");
    }

    /**
     * 根据设备id求设备信息
     * @param eqmtId
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/getEquipmentInfoByEqmtId")
    @ResponseBody
    public Object getEquipmentInfoByEqmtId(@RequestParam(value="eqmtId") String eqmtId) throws Exception {
        BaseEqmtInfoEntity entity = equipmentInfoService.getEquipmentInfoByEqmtId(eqmtId);
        return this.getReturnResult(entity);
    }
    
    /**
     * 更新设备的信息
     * @param entity
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/uptEquipmentInfo")
    @ResponseBody
    public Object uptEquipmentInfo(BaseEqmtInfoEntity entity) throws Exception {
        equipmentInfoService.uptEquipmentInfo(this.getCurrentUser().getUser(), entity);
        return this.getReturnMsg("保存成功");
    }
    
    /**
     * 删除一个设备
     * @param eqmtId
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/delEquipmentInfo")
    @ResponseBody
    public Object delEquipmentInfo(@RequestParam(value="eqmtIds") String[] eqmtIds) throws Exception {
        equipmentInfoService.delEquipmentInfo(this.getCurrentUser().getUser(), eqmtIds);
        return this.getReturnMsg("设备删除成功");
    }

    /**
     * 检验设备关联车辆
     * @param rltvCarId
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @RequestMapping("/validateRltvCarId")
    @ResponseBody
    public Object validateRltvCarId(@RequestParam("rltvCarId") String rltvCarId) throws Exception {
        return this.getReturnResult(equipmentInfoService.validateRltvCarId(rltvCarId));
    }
    
}